# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: student_view.py
6.1 学员视图， 可以注册，选择课程（等同于选择班级）

Author: kij
Create Date: 2021/2/22
-------------------------------------------------
"""
import hashlib

from interface import student_interface, common_interface
from lib import common

logon_state = {"user": None}


def register():
	print("注册".center(50, "="))
	while True:
		user_name = input("请输入用户名:").strip()
		user_password = input("请输入用户密码:").strip()
		user_password2 = input("再次输入用户密码").strip()
		if user_password != user_password2:
			print("两次密码不一样")
			continue
		# 未做加密 未加盐
		m = hashlib.md5(user_password.encode('utf-8'))
		m.update(user_password.encode('utf-8'))
		# m.hexdigest()
		is_success, msg = student_interface.register_interface(user_name, m.hexdigest())
		if is_success:
			print(msg)
			break
		else:
			print(msg)


def logon():
	print("登录".center(50, "="))
	user_name = input("请输入用户名:").strip()
	user_password = input("请输入用户密码:").strip()

	m = hashlib.md5(user_password.encode('utf-8'))
	m.update(user_password.encode('utf-8'))

	exists, data = common_interface.all_logon(user_name, m.hexdigest(), user_type='student')
	if exists:
		print("登录成功")
		logon_state['user'] = user_name
	else:
		print("登录失败")


# 选择课程
@common.logon('student')
def select_lesson():
	# 先选学校
	# 拿到学校信息然后显示让用户选择
	flag, lesson_list = student_interface.get_my_school_lesson(logon_state['user'])
	# 拿到课程列表
	if flag:
		while True:
			for item in range(len(lesson_list)):
				print("%s %s" % (item, lesson_list[item].user_name))
			lesson_num = input("请输入选择的课程:").strip()
			is_success, msssage = student_interface.select_lesson_interface(lesson_list[int(lesson_num)].user_name,
																			logon_state['user'])
			if is_success:
				print(msssage)
				break
			else:
				print(msssage)
	else:
		print(lesson_list)


# 选择学校
@common.logon('student')
def select_school():
	school_list = common_interface.get_all_school()
	for item in range(len(school_list)):
		print("%s %s" % (item, school_list[item].user_name))
	school_num = input("请输入选择的学校:").strip()
	student_interface.select_school_interface(school_list[int(school_num)].user_name, logon_state['user'])


@common.logon('student')
def select_score():
	student_score_dict = student_interface.select_score_interface(logon_state['user'])
	print(student_score_dict)


func_dict = {
	"1": ["注册", register],
	"2": ["登录", logon],
	"3": ["选择学校", select_school],
	"4": ["选择课程", select_lesson],
	"5": ["查看分数", select_score],
}


def show_index():
	while True:
		print("学生视图".center(50, "="))
		for item in func_dict:
			print("%s %s".center(50, " ") % (item, func_dict[item][0]))
		choice = input("输入选项:").strip()
		if not choice.isdecimal():
			continue
		if choice not in ["1", "2", "3", "4", "5"]:
			continue
		func_dict[choice][1]()
